﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraReports.UI;
using System.Drawing;
using System.Data;

namespace ZX.Report
{
    /// <summary>
    /// 列表报表类
    /// </summary>
    public class ListReport : ReportFactory
    {
        /// <summary>
        /// 获取报表
        /// </summary>
        /// <returns></returns>
        public override XtraReport GetReport()
        {
            XtraReport rpt = new XtraReport();// 建立报表实例
            rpt.PaperKind = this.paramsFactory.pagerSize;
            rpt.DataSource = this.paramsFactory.reportDataSource;
            //添加带区（Bands）
            DetailBand detail = new DetailBand();
            PageHeaderBand pageHeader = new PageHeaderBand();
            ReportFooterBand reportFooter = new ReportFooterBand();
            detail.Height = 20;
            reportFooter.Height = 380;
            pageHeader.Height = 20;
            rpt.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { detail, pageHeader, reportFooter });
            
            //添加Styles
            XRControlStyle oddStyle = new XRControlStyle();
            XRControlStyle evenStyle = new XRControlStyle();

            oddStyle.BackColor = Color.LightBlue;
            oddStyle.StyleUsing.UseBackColor = true;
            oddStyle.StyleUsing.UseBorders = false;
            oddStyle.Name = "OddStyle";

            evenStyle.BackColor = Color.LightPink;
            evenStyle.StyleUsing.UseBackColor = true;
            evenStyle.StyleUsing.UseBorders = false;
            evenStyle.Name = "EvenStyle";

            rpt.StyleSheet.AddRange(new DevExpress.XtraReports.UI.XRControlStyle[] { oddStyle, evenStyle });
            //用XRTable显示报表
            DataSet ds = this.paramsFactory.reportDataSource;
            int colCount = ds.Tables[0].Columns.Count;
            int colWidth = 200;

            // Create a table to represent headers
            XRTable tableHeader = new XRTable();
            tableHeader.Height = 40;
            tableHeader.Width = rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right);
            XRTableRow headerRow = new XRTableRow();
            headerRow.Width = tableHeader.Width;
            tableHeader.Rows.Add(headerRow);
            headerRow.BackColor = Color.Gray;
            headerRow.ForeColor = Color.White;

            // Create a table to display data
            XRTable tableDetail = new XRTable();
            tableDetail.Height = 40;
            tableDetail.Width = rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right);
            XRTableRow detailRow = new XRTableRow();
            detailRow.Width = tableDetail.Width; 
            detailRow.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
            tableDetail.Rows.Add(detailRow);
            tableDetail.EvenStyleName = "EvenStyle";
            tableDetail.OddStyleName = "OddStyle";

            // Create table cells, fill the header cells with text, bind the cells to data
            for (int i = 0; i < colCount; i++)
            {
                XRTableCell headerCell = new XRTableCell();
                headerCell.WidthF = colWidth;
                headerCell.Text = ds.Tables[0].Columns[i].Caption;

                XRTableCell detailCell = new XRTableCell();
                detailCell.WidthF = colWidth;
                detailCell.DataBindings.Add("Text", null, ds.Tables[0].Columns[i].Caption);
                if (i == 0)
                {
                    headerCell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;
                    detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;
                }
                else
                {
                    headerCell.Borders = DevExpress.XtraPrinting.BorderSide.All;
                    detailCell.Borders = DevExpress.XtraPrinting.BorderSide.All;
                }

                // Place the cells into the corresponding tables
                headerRow.Cells.Add(headerCell);
                detailRow.Cells.Add(detailCell);
            }

            // Place the table onto a report's Detail band
            rpt.Bands[BandKind.PageHeader].Controls.Add(tableHeader);
            rpt.Bands[BandKind.Detail].Controls.Add(tableDetail);

            return rpt;
        }

        private ListReport(ParamsFactory paramsFactory)
        {
            this.paramsFactory = paramsFactory;
        }
        /// <summary>
        /// 列表报表类实例化
        /// </summary>
        /// <param name="paramsFactory">报表参数</param>
        /// <returns></returns>
        public new static ListReport GetInstance(ParamsFactory paramsFactory)
        {
            return new ListReport(paramsFactory);
        }
    }
}
